package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
	//查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }
    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] menuIds,Integer[] permissionIds){
        //System.out.println(checkGroup);
        //System.out.println(checkitemIds);
        try {
            roleService.add(role,menuIds,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增角色失败");
        }
        return new Result(true,"新增角色成功");
    }
    //回显数据
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role= roleService.findById(id);
            return new Result(true,"查询所有角色成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询所有角色失败");
        }
    }
    //编辑修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] menuIds,Integer[] permissionIds){
        try {
            roleService.edit(role,menuIds,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑角色失败");
        }
        return new Result(true,"编辑角色成功");
    }
    //删除
    @RequestMapping("/deleteRole")
    public Result deleteCheckGroup(Integer id){
        try {
            roleService.deleteRole(id);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除角色失败");
        }
        return new Result(true,"删除角色成功");
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, "查询用户角色成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户角色失败");
        }

    }
}
